/**
 * Created by sxx on 2017/5/6.
 */
import "./index.less";
import attchmentTpl from "./tmpl/attachment.handlebars";
import bankListTpl from "./tmpl/bank_list.handlebars";
import basicInfoTpl from "./tmpl/user_basic_info.handlebars";

jQuery(document).ready(function() {

    App.init();
    let splrId = getArgs().splrId;
    let token = sessionStorage.getItem('supplier_token');
    let regionListObj = localStorage.getItem('regionList');
    let regionList = [];
    let params = {};
    $(".J_return").on("click",function(){
        window.history.go(-1);
    });

    if(regionListObj){
        regionList = JSON.parse(regionListObj);
    }else{
        getRegionInfo();
    }


    const parmTyps = 'CPN_NTR,SPLR_TYP,IDST_TYP,GRUP_ACNT_GRUP,PTNR_TYP';
    const url = HOST_ADDR + '/cpmp/comParm/getByParmTyps/' + parmTyps;
    $.ajax({
        url: HOST_ADDR + '/cpmp/comParm/getByParmTyps/' + parmTyps,
        type: 'get',
        dataType: 'json',
        contentType: 'application/json',
        success: function(resp) {
            params = resp;
            getSplrInfo();
        },
        error: function(err) {
            console.log(err);
        }
    })

    function getSplrInfo(){
        jQuery.ajax({
            url: HOST_ADDR + '/cpmp/splrMng/splr/' + splrId,
            type: 'get',
            dataType: 'json',
            contentType: "application/json",
            headers: {
                'access_token': token,
                'login_type': 'ums'
            },
            success: function (resp) {
                if (resp.success && resp.msgCod=='S0002') {
                    resp.data.hasCreditCode = resp.data.hasCreditCode == '1' ? '是' : '否';
                    $.each(params.data.SPLR_TYP,function(index,item){
                        if (item.parmCod == resp.data.classification) {
                            resp.data.classification = item.parmVal;
                        }
                    });
                    $.each(params.data.CPN_NTR,function(index,item){
                        if (item.parmCod == resp.data.cpnNtr) {
                            resp.data.cpnNtr = item.parmVal
                        }
                    });
                    $.each(params.data.IDST_TYP,function(index,item){
                        if (item.parmCod == resp.data.idst1) {
                            resp.data.idst1 = item.parmVal;
                        }
                        if (item.parmCod == resp.data.idst2) {
                            resp.data.idst2 = item.parmVal;
                        }
                    });
                    $.each(params.data.GRUP_ACNT_GRUP,function(index,item){
                        if (item.parmCod == resp.data.acntGrup) {
                            resp.data.acntGrup = item.parmVal;
                        }
                    });
                    $.each(params.data.PTNR_TYP,function(index,item){
                        if (item.parmCod == resp.data.ptnrTyp) {
                            resp.data.ptnrTyp = item.parmVal;
                        }
                    });

                    resp.data.fullAddr = resp.data.rgstAddr;
                    let region, city, district;
                    let regDate = new Date(resp.data.regTim);
                    if (resp.data.region) {
                        resp.data.region = $.trim(resp.data.region);
                        region = regionList[resp.data.region].name;
                        city = resp.data.city ? regionList[resp.data.region].cities[$.trim(resp.data.city)].name : '';
                        district = (resp.data.city && resp.data.district) ? regionList[resp.data.region].cities[$.trim(resp.data.city)].towns[$.trim(resp.data.district)].name : '';
                        resp.data.fullAddr = region + city + district + (resp.data.rgstAddr ? resp.data.rgstAddr : '');
                    }
                    resp.data.fullTel = '';
                    resp.data.fullFax = '';
                    if(resp.data.telEx || resp.data.telephone){
                        if((resp.data.telEx && !resp.data.telephone) || (!resp.data.telEx && resp.data.telephone)){
                            resp.data.fullTel = resp.data.telEx ? resp.data.telEx : resp.data.telephone;
                        }else{
                            resp.data.fullTel = resp.data.telEx + ' - ' + resp.data.telephone;
                        }
                    }
                    if(resp.data.faxEx || resp.data.fax){
                        if((resp.data.faxEx && !resp.data.fax) || (!resp.data.faxEx && resp.data.fax)){
                            resp.data.fullFax = resp.data.faxEx ? resp.data.faxEx : resp.data.fax;
                        }else{
                            resp.data.fullFax = resp.data.faxEx + ' - ' + resp.data.fax;
                        }
                    }

                    let basicInfo = basicInfoTpl(resp);
                    $("#basicInfo").html(basicInfo);
                    $(".splr_nam").html(resp.data.fullNam);
                    $("#bankList tbody").html("").parent().parent().addClass("hide");
                    $("#attachmentInfo").html("").parent().addClass("hide");
                    if(resp.data.splrBnkAcnts.length){
                        let bankList = bankListTpl({bankList:resp.data.splrBnkAcnts});
                        $("#bankList tbody").html(bankList).parent().parent().removeClass("hide");
                    }
                    if(resp.data.splrApts.length){
                        let attchments = attchmentTpl({aptList:resp.data.splrApts});
                        $("#attachmentInfo").html(attchments).parent().removeClass("hide");
                    }
                }else{
                    console.log(resp.msgCod + ' ' + resp.msgInf);
                }
            },
            error: function (err) {
                console.log(err);
            }
        });
    };

    // 获取地区信息
    function getRegionInfo(){
        $.ajax({
            url: HOST_ADDR + '/cpmp/region/region_all',
            type: 'get',
            async:true,
            dataType: 'json',
            contentType: "application/json",
            success: function(resp) {
                localStorage.setItem('regionList', JSON.stringify(resp));
                regionList = resp;
            },
            error: function(err) {
                console.log(err);
            }
        });
    };
});